﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ChineseSchool.Models.Hethong;
using ChineseSchool.BLL;
using System.Data;
using System.Net;

namespace ChineseSchool.Admin
{
    public partial class QL_Activities : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BinGrid();
                btnAdd.Visible = true;
                btnEdit.Visible = true;
            }
        }

        protected void BinGrid()
        {
            var bActivities = new B_Activities();
            var lstMActivities = bActivities.GetAlls(0);
            grdActivities.DataSource = lstMActivities;
            grdActivities.DataBind();
        }

        protected void grdActivities_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            grdActivities.PageIndex = e.NewPageIndex;
            BinGrid();
        }

        protected void grdActivities_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            var activitiesId = Convert.ToInt32(e.CommandArgument);
            var mActivities = new M_Activities();
            var bActivities = new B_Activities();
            var bInsertUpdateDelete = new B_Insert_Update_Delete();
            if (e.CommandName.Equals("Delete", StringComparison.OrdinalIgnoreCase))
            {
                try
                {
                    bInsertUpdateDelete.delete("Activities", "ActivitiesID", mActivities, activitiesId);
                    Response.Write("<script>alert('Đã được xóa!')</script>");
                }
                catch (Exception ex)
                {
                    throw new Exception("Đã có lỗi xảy ra! Vui lòng thử lại lần sau!", ex);
                }
                BinGrid();
            }
            if (e.CommandName.Equals("Edit", StringComparison.OrdinalIgnoreCase))
            {
                panAdd.Visible = true;
                btnAdd.Visible = false;
                lblTitle.Text = "Cập nhật";
                mActivities = bActivities.GetByPK(activitiesId);
                txtTitle.Text = mActivities.ActivitiesTitle;
                txtActivitiesDate.Text = mActivities.ActivitiesDate.ToString("dd/MM/yyyy");
                ckActivitiesContent.Value = mActivities.ActivitiesContent;
                btnEdit.CommandArgument = activitiesId.ToString();
            }
        }

        protected void btnNew_Click(object sender, EventArgs e)
        {
            panAdd.Visible = true;
            btnEdit.Visible = false;
            lblTitle.Text = "Thêm mới";
            txtTitle.Text = "";
            txtActivitiesDate.Text = "";
            ckActivitiesContent.Value = "";
        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            var bInsertUpdateDelete = new B_Insert_Update_Delete();
            panAdd.Visible = false;
            btnEdit.Visible = true;
            /*tải ảnh */
            var imageUrl = "";
            var file2 = fuImageUrl.PostedFile;

            if (fuImageUrl.HasFile == false && file2.ContentLength > 500000)
            {
                lblThongbao.Text = "File nặng quá T_T";
            }
            else
            {
                try
                {
                    var path2 = Server.MapPath("/admin/images/Upload/" + fuImageUrl.FileName);
                    fuImageUrl.SaveAs(path2);
                    imageUrl = "/admin/images/Upload/" + fuImageUrl.FileName;
                    lblThongbao.Text = "Tải thành công";
                }
                catch
                {
                    lblThongbao.Text = "Tải ảnh thất bại! vui lòng tải lại";
                }
            }

            /*tải ảnh */
            var mActivities = new M_Activities
            {
                ActivitiesTitle =txtTitle.Text,
                ActivitiesDate = DateTime.ParseExact(txtActivitiesDate.Text, "dd/MM/yyyy", null),
                ImageUrl = imageUrl,
                ActivitiesContent = ckActivitiesContent.Value
            };
            //string format = "ddd, dd/MM/yyyy h:mm:ss tt";
            //M_Activities.postDate = DateTime.ParseExact(DateTime.Now.ToString(format), "ddd, dd/MM/yyyy h:mm:ss tt", null);
            bInsertUpdateDelete.insert("Activities", "ActivitiesID", mActivities);
            panAdd.Visible = false;
            BinGrid();
        }

        protected void btnEdit_Click(object sender, EventArgs e)
        {
            var bInsertUpdateDelete = new B_Insert_Update_Delete();
            panAdd.Visible = false;
            btnAdd.Visible = true;
            var imageUrl = "";
            var mActivities = new M_Activities();
            var bActivities = new B_Activities();
            var activitiesId = Convert.ToInt32(btnEdit.CommandArgument);
            var file2 = fuImageUrl.PostedFile;
            if (fuImageUrl.HasFile)
            {
                if (fuImageUrl.HasFile == false && file2.ContentLength > 500000)
                {
                    lblThongbao.Text = "File nặng quá T_T";
                }
                else
                {
                    try
                    {
                        var path2 = Server.MapPath("/admin/images/Upload/" + fuImageUrl.FileName);
                        fuImageUrl.SaveAs(path2);
                        imageUrl = "/admin/images/Upload/" + fuImageUrl.FileName;
                        lblThongbao.Text = "Tải thành công";
                    }
                    catch
                    {
                        lblThongbao.Text = "Tải ảnh thất bại! vui lòng tải lại";
                    }
                }
            }
            else
            {
                mActivities = bActivities.GetByPK(activitiesId);
                imageUrl = mActivities.ImageUrl;
            }
            /*tải ảnh */
            mActivities.ActivitiesID = Convert.ToInt32(btnEdit.CommandArgument);
            mActivities.ActivitiesTitle = txtTitle.Text;
            mActivities.ActivitiesDate = DateTime.ParseExact(txtActivitiesDate.Text, "dd/MM/yyyy", null);
            mActivities.ImageUrl = imageUrl;
            //string format = "ddd, dd/MM/yyyy h:mm:ss tt";
            //M_Activities.postDate = DateTime.ParseExact(DateTime.Now.ToString(format), "ddd, dd/MM/yyyy h:mm:ss tt", null);
            mActivities.ActivitiesContent = ckActivitiesContent.Value;
            bInsertUpdateDelete.update("Activities", "ActivitiesID", mActivities, activitiesId);
            panAdd.Visible = false;
            BinGrid();
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            panAdd.Visible = false;
            btnAdd.Visible = true;
            btnEdit.Visible = true;
            txtTitle.Text = "";
            txtActivitiesDate.Text = "";
            ckActivitiesContent.Value = "";
            BinGrid();
        }

        protected void grdActivities_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            BinGrid();
        }

        protected void grdActivities_RowEditing(object sender, GridViewEditEventArgs e)
        {
            grdActivities.EditIndex = e.NewEditIndex;
            BinGrid();
        }
    }
}